package com.hngy.educationaladministration.service.serviceImpl;

import com.hngy.educationaladministration.service.UpdateSerivce;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * @author Deng JunHao
 * @date 2021/04/12 11:09
 * @description：
 **/
@Service

public class UpdateSerivcelmpl implements UpdateSerivce {
    /**
     * 上传文件
     *
     * @param file
     * @param session
     * @return
     * @throws IOException
     */
    @Override
    public Boolean upload(MultipartFile file, HttpSession session) throws IOException {
        //  1、指定上传的文件夹
        String path = session.getServletContext().getRealPath("/upload");
        System.out.println(path);
        //  2、判断文件夹是否存在，如果不存在则创建该文件夹
        File uploadFilePath = new File(path);
        if (!uploadFilePath.exists()) {
            uploadFilePath.mkdir();
        }

        //  3、重命名
        String fileName = file.getOriginalFilename();

        String ext = fileName.substring(fileName.lastIndexOf("."));

        long time = System.currentTimeMillis();

        String newFileName = time + ext;

        File destFile = new File(path + "/" + newFileName);
        //  4、上传
        file.transferTo(destFile);
        //  5、返回上传路径

        //上传到对应uid
        String user_photo = "upload/" + newFileName;


        //把user_photo存到user中即可
//        Integer uid = Integer.parseInt(session.getAttribute("uid").toString());
//        userService.uploaduser_photo(uid,user_photo);
//        session.setAttribute("user_photo",user_photo);

        if (0 != user_photo.length()) {
            return true;
        } else {
            return false;
        }
    }
}
